package com.minhua.jijiehao2.mainframe.launcher.loginactivity.impl

import android.app.Activity
import android.content.Intent
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.View
import com.minhua.jijiehao2.R
import com.minhua.jijiehao2.base.BaseActivity
import com.minhua.jijiehao2.constants.DefineKey
import com.minhua.jijiehao2.constants.HttpConst
import com.minhua.jijiehao2.mainframe.launcher.forgetpasswordactivity.impl.ForgetPasswordActivity
import com.minhua.jijiehao2.mainframe.launcher.loginactivity.imp.ILoginView
import com.minhua.jijiehao2.mainframe.launcher.registeractivity.impl.RegisterActivity
import com.minhua.jijiehao2.mainframe.main.impl.MainActivity
import com.minhua.jijiehao2.utils.ActivityTaskManager
import com.minhua.jijiehao2.utils.LoadingDialogUtil
import com.minhua.jijiehao2.utils.SPUtils
import com.minhua.jijiehao2.utils.T
import kotlinx.android.synthetic.main.activity_login.*

internal class LoginActivity : BaseActivity<ILoginView, LoginPresenter>(), ILoginView {
    override fun getLayoutId(): Int = R.layout.activity_login

    override fun getPresenter(): LoginPresenter = LoginPresenter()

    companion object {
        fun logged(activity: Activity): Boolean {
            val logged: Boolean = SPUtils.get(DefineKey.LOGIN_STATE, false) as Boolean
            if (!logged) {
                activity.startActivity(Intent(activity, LoginActivity::class.java))
            }
            return logged
        }
    }

    private val onClickListener: View.OnClickListener = View.OnClickListener { v ->
        when (v.id) {
            R.id.tvLogin -> mPresenter.isPhoneExists()
            R.id.ivBack -> finish()
            R.id.forgetPassword -> startActivity(Intent(this, ForgetPasswordActivity::class.java))
            R.id.registerSoon -> RegisterActivity.intentStart(this, getUserName())
        }
    }

    private val textWacher: TextWatcher = object : TextWatcher {
        override fun afterTextChanged(s: Editable?) {
        }

        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
        }

        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            tvLogin.isEnabled = !(TextUtils.isEmpty(phone.text) || TextUtils.isEmpty(password.text))
        }
    }

    override fun initView() {
        tvLogin.setOnClickListener(onClickListener)
        ivBack.setOnClickListener(onClickListener)
        forgetPassword.setOnClickListener(onClickListener)
        registerSoon.setOnClickListener(onClickListener)
        phone.editText.addTextChangedListener(textWacher)
        password.editText.addTextChangedListener(textWacher)
    }

    override fun getUserName(): String = phone.text

    override fun getPassword(): String = password.text

    override fun loginSuccess(toWhere: String) {
        if (HttpConst.STATE_10 == toWhere) {
            T.showShort("该账号还未注册")
            LoadingDialogUtil.dismissDialog()
            android.os.Handler().postDelayed({
                RegisterActivity.intentStart(this, getUserName())
            }, 500)
        } else {
            T.showLong("登录成功")
            android.os.Handler().postDelayed({
                ActivityTaskManager.getActivityTaskManager().finishAllActivity()
                startActivity(Intent(this, MainActivity::class.java))
            }, 500)
        }
    }

    override fun onUserNameError() {
        T.showShort("请输入正确的手机号")
    }

    override fun onPasswordError() {
        T.showShort("密码由6-20位数字字母组成")
    }
}
